import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    // host: '0.0.0.0',
    port: 5174,
    // https: true,
    // open: true,
    proxy: {
      // proxyRefs: 'https',
      '/api': {
        //路径中包含api的请求
        target: 'http://localhost:10000', //后台服务器所在的源
        changeOrigin: true, // 修改源
        rewrite: (path) => path.replace(/^\/api/, ''), // 把api替换为空的字符串
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyRefs, req, res) => {
            proxyRefs.removeHeader('origin')
          })
        }
      }
    }
  }
})
